如何用matplotlib做colormap来给表格单元格背景着色?

您所在的位置:网站首页 matplotlib 导出图片 如何用matplotlib做colormap来给表格单元格背景着色?

如何用matplotlib做colormap来给表格单元格背景着色?

2023-04-15 16:45| 来源: 网络整理| 查看: 265

对于单元格的背景色,你可以使用你想装饰的相应的颜色列表,或者你可以使用颜色地图按数据值创建一个颜色列表来装饰。对于后一种方法,我尝试了seaborn的调色板和matplotlib的方法。seaborn的调色板被应用于HS列,因为数据是按升序排列的。在一个新的列中,按原顺序重新排序。然后用重新排序的列表来装饰目标列。

import matplotlib.pyplot as plt import pandas as pd import seaborn as sns fig, ax =plt.subplots(1,1) ax.axis('tight') ax.axis('off') table_data = pd.read_csv("test.csv") table_data =table_data.astype({"Runs":int, "HS":int}) table_data =table_data.sort_values(by='HS') df = table_data table = ax.table(cellText=df.values, colLabels=df.columns, loc="center", cellLoc="left") table.auto_set_column_width(col=list(range(len(df.columns)))) # sns color palette use(data:ascending) bcmap = sns.mpl_palette("Blues", len(table_data['HS'].unique())) for idx,b in enumerate(bcmap): table[(idx+1, 6)].set_facecolor(b) # matplotlib colormap from matplotlib import cm vals = table_data['S/R'] normal = cm.colors.Normalize(vals.min(), vals.max()) bcmap2 = plt.cm.Blues(normal(vals)) for idx, bb in enumerate(bcmap2): table[(idx+1, 10)].set_facecolor(bb) plt.show()

enter image description here



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3